package baidu;

import org.apache.commons.io.FileUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import util.CosineSimilarityAlgorithm;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.*;

/**
 * Created by zhongwei on 2017/1/5.
 */
public class Entry extends Thread {
    private static final String QUERY = "http://baike.baidu.com/search/word?word=[KEYWORD]";
    static FileWriter fileWriter = null;
    static Set<String> set = new HashSet<>();
    private static HttpClient client = HttpClients.createDefault();
    private static RequestConfig requestConfig = null;

    @Override
    public void run() {
        try {
            main(null);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws URISyntaxException, IOException {
        requestConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(5000).build();
        File result_file = new File("baidu_result.txt");
        fileWriter = new FileWriter(result_file, true);
        String content = FileUtils.readFileToString(result_file);
        String[] dones = content.split("==========\n");
        for (String str : dones) {
            if (str.trim().length() > 0) {
                set.add(str.split("\n")[0]);
            }
        }
        List<String> peoples = FileUtils.readLines(new File("peoples"));
        int i = set.size();
        int total = peoples.size();
        for (String str : peoples) {
            if (set.contains(str))
                continue;
            System.out.println("baidu process : " + i++ + " / " + total);
            String[] arr = str.split("\t");
            if (arr.length == 2) {
                String people = arr[0];
                String url = QUERY.replace("[KEYWORD]", URLEncoder.encode(people));
                String page1 = download(url);
                judge(page1, str);
            }
        }
        System.out.println("baidu all is done");
        fileWriter.close();
    }

    private static String download(String url) {
        try {
            HttpGet get = new HttpGet(url);
            get.setConfig(requestConfig);
            HttpResponse response = client.execute(get);
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            EntityUtils.consumeQuietly(response.getEntity());
            return content;
        } catch (Exception e) {
            return download(url);
        }
    }

    private static void judge(String page1, String people) throws IOException {
        if (page1.contains("百度百科尚未收录词条")) {
            // 没有找到词条就不用进一步处理
            fileWriter.write(people + "\n\n");
            fileWriter.write("==========\n");
            fileWriter.flush();
        } else if (page1.contains("多义词") && page1.contains("请在下列") && page1.contains("义项") && page1.contains("上选择浏览")) {
            // 多义词，需要根据公司名称进一步过滤
            processMultiSemantic(page1, people);
        } else {
            processDetail(page1, people);
        }
    }

    private static void processMultiSemantic(String page1, String people) throws IOException {
        Document document = Jsoup.parse(page1);
        Elements lis = null;
        try {
            lis = document.getElementsByClass("polysemantList-wrapper").get(0).getElementsByTag("li");
        } catch (Exception e) {
            lis = document.getElementsByClass("custom_dot").get(0).getElementsByClass("para");
        }
        final String[] arr = people.split("\t");
        Collections.sort(lis, new Comparator<Element>() {
            @Override
            public int compare(Element o1, Element o2) {
                double diff = CosineSimilarityAlgorithm.getSimilarity(o2.text(), arr[1]) - CosineSimilarityAlgorithm.getSimilarity(o1.text(), arr[1]);
                if (diff == 0d) return 0;
                else if (diff > 0d) return 1;
                else return -1;
            }
        });
        double maxSim = CosineSimilarityAlgorithm.getSimilarity(lis.get(0).text(), arr[1]);
        if (maxSim < 0.2d) {
            fileWriter.write(people + "\n\n");
            fileWriter.write("==========\n");
            fileWriter.flush();
        } else {
            try {
                String url = "http://baike.baidu.com" + lis.get(0).getElementsByTag("a").get(0).attr("href");
                String detailPage = download(url);
                processDetail(detailPage, people);
            } catch (Exception e) {
                // 已经选中目标记录
                processDetail(page1, people);
            }
        }
    }

    private static void processDetail(String page1, String people) throws IOException {
        Document document = Jsoup.parse(page1);
        try {
            Elements eles = document.getElementsByClass("basic-info").get(0).getElementsByClass("basicInfo-item");
            fileWriter.write(people + "\n");
            int size = eles.size();
            for (int i = 0; i < size; ) {
                fileWriter.write(eles.get(i).text().replaceAll("    ", "") + ":" + eles.get(i + 1).text() + "\t\t");
                i = i + 2;
            }
            fileWriter.write("\n==========\n");
            fileWriter.flush();
        } catch (Exception e) {
            // 没有基本信息一栏
            fileWriter.write(people + "\n\n");
            fileWriter.write("==========\n");
            fileWriter.flush();
        }
    }

}
